﻿#region Đào Mạnh Hòa
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.Collections;
using DataAccess;
using Entities;
using Common;


namespace BusinessLogic
{
    public static class ChiTietHDBanBuon
    {
        static SqlCommand cmd;
        static ArrayList arr;
        static SqlDataReader dr;
        static SqlConnection cn;

        #region select
        public static Entities.ChiTietHDBanBuon[] Select()
        {
            Entities.ChiTietHDBanBuon[] list = null;
            try
            {
                string sql = Common.Constants.ChiTietHDBanBuon.Select;
                cn = DataAccess.Connection.OpenConnection();
                cmd = new SqlCommand(sql, cn);
                cmd.CommandType = CommandType.StoredProcedure;
                dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                arr = new ArrayList();
                while (dr.Read())
                {
                    Entities.ChiTietHDBanBuon ChiTietHDBanBuon = new Entities.ChiTietHDBanBuon();
                    ChiTietHDBanBuon.MaHDBanBuon = dr[Common.Constants.ChiTietHDBanBuon.MaHDBanBuon].ToString();
                    ChiTietHDBanBuon.MaHangHoa = dr[Common.Constants.ChiTietHDBanBuon.MaHangHoa].ToString();
                    ChiTietHDBanBuon.TenHangHoa = dr[Common.Constants.ChiTietHDBanBuon.TenHangHoa].ToString();
                    ChiTietHDBanBuon.SoLuong = int.Parse(dr[Common.Constants.ChiTietHDBanBuon.SoLuong].ToString());
                    ChiTietHDBanBuon.DonGia = (dr[Common.Constants.ChiTietHDBanBuon.DonGia].ToString());
                    ChiTietHDBanBuon.ThanhTien = (dr[Common.Constants.ChiTietHDBanBuon.ThanhTien].ToString());
                    ChiTietHDBanBuon.ThueGTGT = (dr[Common.Constants.ChiTietHDBanBuon.ThueGTGT].ToString());
                    ChiTietHDBanBuon.PhanTramChietKhau = dr[Common.Constants.ChiTietHDBanBuon.PhanTramChietKhau].ToString();
                    ChiTietHDBanBuon.GhiChu = dr[Common.Constants.ChiTietHDBanBuon.GhiChu].ToString();
                    ChiTietHDBanBuon.Deleted = bool.Parse(dr[Common.Constants.ChiTietHDBanBuon.Deleted].ToString());
                    arr.Add(ChiTietHDBanBuon);
                }
                int n = arr.Count;
                if (n == 0)
                {
                    list = new Entities.ChiTietHDBanBuon[0];
                }
                else
                {
                    list = new Entities.ChiTietHDBanBuon[n];
                    for (int i = 0; i < n; i++)
                    {
                        list[i] = (Entities.ChiTietHDBanBuon)arr[i];
                    }
                }
                return list;
            }
            catch (Exception ex)
            { string s = ex.Message.ToString(); return new Entities.ChiTietHDBanBuon[0]; }
            finally
            {
                cmd.Connection.Dispose();
                cn.Close();
            }

        }
        #endregion

        #region insert update
        public static int InsertUpdate(Entities.ChiTietHDBanBuon chitiethdbanbuon)
        {
            int rt = 0;
            try
            {
                string sql = Common.Constants.ChiTietHDBanBuon.InsertUpdate;
                cn = DataAccess.Connection.OpenConnection();
                SqlCommand cmd = new SqlCommand(sql, cn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(Common.Constants.ChiTietHDBanBuon.HanhDong, SqlDbType.NVarChar, 50).Value = chitiethdbanbuon.HanhDong;
                cmd.Parameters.Add(Common.Constants.ChiTietHDBanBuon.MaHDBanBuon, SqlDbType.VarChar, 50).Value = chitiethdbanbuon.MaHDBanBuon;
                cmd.Parameters.Add(Common.Constants.ChiTietHDBanBuon.MaHangHoa, SqlDbType.VarChar, 50).Value = chitiethdbanbuon.MaHangHoa;
                cmd.Parameters.Add(Common.Constants.ChiTietHDBanBuon.TenHangHoa, SqlDbType.NVarChar, 200).Value = chitiethdbanbuon.TenHangHoa;
                cmd.Parameters.Add(Common.Constants.ChiTietHDBanBuon.SoLuong, SqlDbType.Int).Value = chitiethdbanbuon.SoLuong;
                cmd.Parameters.Add(Common.Constants.ChiTietHDBanBuon.SoLuongConLai, SqlDbType.Int).Value = chitiethdbanbuon.SoLuongConlai;
                cmd.Parameters.Add(Common.Constants.ChiTietHDBanBuon.DonGia, SqlDbType.Float).Value = chitiethdbanbuon.DonGia;
                cmd.Parameters.Add(Common.Constants.ChiTietHDBanBuon.PhanTramChietKhau, SqlDbType.Float).Value = chitiethdbanbuon.PhanTramChietKhau;
                cmd.Parameters.Add(Common.Constants.ChiTietHDBanBuon.ThueGTGT, SqlDbType.Float).Value = chitiethdbanbuon.ThueGTGT;
                cmd.Parameters.Add(Common.Constants.ChiTietHDBanBuon.ThanhTien, SqlDbType.Float).Value = chitiethdbanbuon.ThanhTien;
                cmd.Parameters.Add(Common.Constants.ChiTietHDBanBuon.GhiChu, SqlDbType.NVarChar, 200).Value = chitiethdbanbuon.GhiChu;
                cmd.Parameters.Add(Common.Constants.ChiTietHDBanBuon.Deleted, SqlDbType.Bit).Value = chitiethdbanbuon.Deleted;
                rt = cmd.ExecuteNonQuery();

                cn.Close();
                return rt;
            }
            catch (Exception ex)
            { string s = ex.Message.ToString(); return 0; }

        }

        public static int InsertUpdate(Entities.ChiTietHDBanBuon[] chitiethdbanbuon)
        {
            try
            {
                foreach (Entities.ChiTietHDBanBuon item in chitiethdbanbuon)
                {
                    if (InsertUpdate(item) == 0)
                    {
                        return 0;
                    }
                }
                return 1;
            }
            catch
            {
                return 0;
            }
        }
        #endregion
    }
}
#endregion